﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using Microsoft.VisualBasic.Devices;
using System.Threading;

namespace WeiSiCopy
{
    public class SiCopy
    {

        private Apply m_apply;

        public static ManualResetEvent eventX;
        public static int iCount = 0;
        public static int MaxCount;

        public SiCopy(Apply apply)
        {
           
            m_apply = apply;
        }

        public bool Run()
        {
            bool flag = false;

            Computer MyComputer = new Computer();
            try
            {

                MyComputer.FileSystem.CopyDirectory(
                  BaseSetting.SitePath + m_apply.SrcSiName,
                  BaseSetting.SitePath + m_apply.DesSiName);

                FileHandle fh = new FileHandle(m_apply);
                flag = fh.ModitfyConfig(BaseSetting.SitePath + m_apply.DesSiName + "\\configuration.php");

                MyComputer.FileSystem.CopyDirectory(
                  BaseSetting.MySQLPath + m_apply.SrcSiName,
                  BaseSetting.MySQLPath + m_apply.DesSiName);

                Log.WriteLog(m_apply.DesSiName + " success!", true);

                m_apply.status = 2;
                m_apply.Update();


            }
            catch (Exception ex)
            {
                m_apply.status = 3;
                m_apply.Update();

                Log.WriteLog(m_apply.DesSiName + " copy error!", true);
            }
            finally
            {
                Interlocked.Increment(ref iCount);
                if (iCount == MaxCount)
                {
                    eventX.Set();
                }
            }


            return flag;
        }

    }
}
